<?php
namespace app\api\controller;
use app\common\controller\Api;

/**
 * 商品订单模块
 * User: LK
 * Date: 2021/3/24
 */
class Order extends Api
{
    protected $noNeedLogin = ['getCouponExchangeList'];
    protected $noNeedRight = ['*'];


    /**
     * 获取兑换活动列表
     * @ApiMethod GET
     */
    public function getCouponExchangeList(\app\common\model\CouponExchangeModel $CouponExchangeModel)
    {
        $user_id = $this->auth->id;
        $return = $CouponExchangeModel->getList();
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 商品确认订单
     *
     * @param string $use_type 使用位置:mini=小程序,pc=PC
     * @param int $user_address_id 用户地址ID
     * @param int $user_coupon_id 优惠券ID
     * @param array $goods_params 商品信息
//     * @param int $goods_id 商品ID
//     * @param int $goods_spec_id 规格ID
//     * @param int $num 数量
     * @param string $dispatch_type 发货方式:express=物流快递,ziti=自提
     */
    public function confirmOrder(\app\common\model\order\OrderModel $OrderModel, \app\api\validate\OrderValidate $OrderValidate)
    {
        $params = $this->request->post();
        if(!$OrderValidate->check($params,[],'confirmOrder')){
            $this->errorReturn(40001,$params,$OrderValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $OrderModel->confirmOrder($params, $user_id, 'json');
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 商品确认订单
     *
     * @param string $use_type 使用位置:mini=小程序,pc=PC
     * @param int $user_address_id 用户地址ID
     * @param int $user_coupon_id 优惠券ID
     * @param int $goods_id 商品ID
     * @param int $goods_spec_id 规格ID
     * @param int $num 数量
     * @param string $dispatch_type 发货方式:express=物流快递,ziti=自提
     */
    public function goodsConfirmOrder(\app\common\model\order\OrderModel $OrderModel, \app\api\validate\OrderValidate $OrderValidate)
    {
        $params = $this->request->post();
        if(!$OrderValidate->check($params,[],'goodsConfirmOrder')){
            $this->errorReturn(40001,$params,$OrderValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $OrderModel->confirmOrder($params, $user_id, 'goods');
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 购物车确认订单
     *
     * @param string $use_type 使用位置:mini=小程序,pc=PC
     * @param string $cart_ids 购物车ID集
     * @param int $user_address_id 用户地址ID
     * @param int $user_coupon_id 优惠券ID
     * @param string $dispatch_type 发货方式:express=物流快递,ziti=自提
     *
     */
    public function cartConfirmOrder(\app\common\model\order\OrderModel $OrderModel, \app\api\validate\OrderValidate $OrderValidate)
    {
        $params = $this->request->post();
        if(!$OrderValidate->check($params,[],'cartConfirmOrder')){
            $this->errorReturn(40001,$params,$OrderValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $OrderModel->confirmOrder($params, $user_id, 'cart');
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 下单
     *
     * @param string $place_type 下单类型:goods=单商品,cart=购物车
     * @param string $use_type 平台:mini=小程序,pc=PC,app=App
     * @param string $remark 买家备注
     * @param int $user_address_id 用户地址ID
     * @param int $user_coupon_id 优惠券ID
     * @param string $dispatch_type 发货方式:express=物流快递,ziti=自提
     * @param int $coupon_exchange_id 兑换活动ID
     *
     * $place_type = json
     * @param array $goods_params 商品信息
     *
     * $place_type = goods
     * @param int $goods_id 商品ID
     * @param int $goods_spec_id 规格ID
     * @param int $num 数量
     *
     * $place_type = cart
     * @param string $cart_ids 购物车ID集
     *
     */
    public function placeOrder(\app\common\model\order\OrderModel $OrderModel, \app\api\validate\OrderValidate $OrderValidate)
    {
        $params = $this->request->post();
        if(!$OrderValidate->check($params,[],'placeOrder')){
            $this->errorReturn(40001,$params,$OrderValidate->getError());
        }
        $user_id = $this->auth->id;
        $openid = $this->auth->openid;
        $return = $OrderModel->placeOrder($params, $user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 订单列表
     *
     * @param int $status 订单状态:0=全部,1=待支付,2=待收货,3=已完成(0=待支付,1=待发货,2=待收货,3=待评论,4=已完成,5=已取消)
     * @param int $page 页码
     * @param int $limit 每页数据
     * @param string $search
     */
    public function getOrderPageList(\app\common\model\order\OrderModel $OrderModel)
    {
        $params = $this->request->post();
        $user_id = $this->auth->id;
        $return = $OrderModel->getOrderPageList($params,$user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 订单详情
     *
     * @param int $order_id 订单ID
     */
    public function getOrderDetail(\app\common\model\order\OrderModel $OrderModel, \app\api\validate\OrderValidate $OrderValidate)
    {
        $params = $this->request->post();
        if(!$OrderValidate->check($params,[],'getOrderDetail')){
            $this->errorReturn(40001,$params,$OrderValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $OrderModel->getOrderDetail($params, $user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 取消订单
     *
     * @param int $order_id 订单ID
     */
    public function cancelOrder(\app\common\model\order\OrderModel $OrderModel, \app\api\validate\OrderValidate $OrderValidate)
    {
        $params = $this->request->post();
        if(!$OrderValidate->check($params,[],'cancelOrder')){
            $this->errorReturn(40001,$params,$OrderValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $OrderModel->cancelOrder($params, $user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 确认收货
     *
     * @param int $order_id 订单ID
     */
    public function confirmReceipt(\app\common\model\order\OrderModel $OrderModel, \app\api\validate\OrderValidate $OrderValidate)
    {
        $params = $this->request->post();
        if(!$OrderValidate->check($params,[],'confirmReceipt')){
            $this->errorReturn(40001,$params,$OrderValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $OrderModel->confirmReceipt($params, $user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 售后申请信息
     *
     * @param int $order_id 订单ID
     * @param int $order_goods_id 订单商品ID
    */
    public function applyRefundInfo(\app\common\model\order\OrderModel $OrderModel, \app\api\validate\OrderValidate $OrderValidate)
    {
        $params = $this->request->post();
        if(!$OrderValidate->check($params,[],'applyRefundInfo')){
            $this->errorReturn(40001,$params,$OrderValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $OrderModel->applyRefundInfo($params, $user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 售后申请
     *
     * @param int $type 售后类型:1=退款,2=退货退款
     * @param int $order_id 订单ID
     * @param int $order_goods_id 订单商品ID
     * @param int $num 售后数量
     * @param int $refund_price 退款金额
     * @param string $cause 售后原因
//     * @param string $content 售后描述
     * @param string $images 图片集
    */
    public function applyRefund(\app\common\model\order\OrderModel $OrderModel, \app\api\validate\OrderValidate $OrderValidate)
    {
        $params = $this->request->post();
        if(!$OrderValidate->check($params,[],'applyRefund')){
            $this->errorReturn(40001,$params,$OrderValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $OrderModel->applyRefund($params, $user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 删除订单
     *
     * @param int $order_id 订单ID
     */
    public function deleteOrder(\app\common\model\order\OrderModel $OrderModel, \app\api\validate\OrderValidate $OrderValidate)
    {
        $params = $this->request->post();
        if(!$OrderValidate->check($params,[],'deleteOrder')){
            $this->errorReturn(40001,$params,$OrderValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $OrderModel->deleteOrder($params, $user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 获取订单物流包裹信息
     *
     * @param int $order_id 订单ID
     */
    public function getExpressList(\app\common\model\order\OrderExpress $OrderExpress, \app\api\validate\OrderValidate $OrderValidate)
    {
        $params = $this->request->get();
        if(!$OrderValidate->check($params,[],'getExpressList')){
            $this->errorReturn(40001,$params,$OrderValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $OrderExpress->getExpressList($params, $user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 查询物流信息
     *
     * @param int $order_express_id 包裹ID
     */
    public function express(\app\common\model\order\OrderExpress $OrderExpress, \app\api\validate\OrderValidate $OrderValidate)
    {
        $params = $this->request->get();
        if(!$OrderValidate->check($params,[],'express')){
            $this->errorReturn(40001,$params,$OrderValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $OrderExpress->express($params, $user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 订单状态角标数量
     * @ApiMethod GET
     */
    public function getOrderStatusNum(\app\common\model\order\OrderModel $OrderModel)
    {
        $user_id = $this->auth->id;
        $return = $OrderModel->getOrderStatusNum($user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

}